package bb.lanxing.lib.devices.core.scanner;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.os.Build;
import android.os.ParcelUuid;
import android.text.TextUtils;
import bb.lanxing.lib.devices.core.scanner.DeviceScanner;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class BleDeviceScanner extends AbstractDeviceScanner<ScannedDeviceResult<BluetoothDevice>> {
    private ScanCallback scanCallbackForHigherThan20;
    private BluetoothAdapter.LeScanCallback scanCallbackForLowerThan21;

    protected UUID[] getFilterUUIDS() {
        return null;
    }

    public BleDeviceScanner(Context context, DeviceScanner.ScannerCallback<ScannedDeviceResult<BluetoothDevice>> scannerCallback) {
        super(context, scannerCallback);
    }

    @Override
    protected boolean doStartScan() {
        return startScanWithBluetooth();
    }

    @Override
    public boolean stopScan() {
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter != null && defaultAdapter.isEnabled()) {
            if (this.scanCallbackForHigherThan20 != null) {
                BluetoothLeScanner bluetoothLeScanner = defaultAdapter.getBluetoothLeScanner();
                if (bluetoothLeScanner != null) {
                    bluetoothLeScanner.stopScan(this.scanCallbackForHigherThan20);
                }
            } else {
                BluetoothAdapter.LeScanCallback leScanCallback = this.scanCallbackForLowerThan21;
                if (leScanCallback != null) {
                    defaultAdapter.stopLeScan(leScanCallback);
                }
            }
        }
        return super.stopScan();
    }

    @SuppressLint("MissingPermission")
    private boolean startScanWithBluetooth() {
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter == null || !defaultAdapter.isEnabled()) {
            return false;
        }
        UUID[] filterUUIDS = getFilterUUIDS();
        BluetoothLeScanner bluetoothLeScanner = defaultAdapter.getBluetoothLeScanner();
        if (bluetoothLeScanner != null) {
            if (this.scanCallbackForHigherThan20 == null) {
                this.scanCallbackForHigherThan20 = new ScanCallback() {
                    @Override
                    public void onScanFailed(int i) {
                        BleDeviceScanner.this.stopScan();
                    }

                    @Override
                    public void onScanResult(int i, ScanResult scanResult) {
                        BleDeviceScanner.this.handleDeviceForBle(scanResult.getDevice(), scanResult.getRssi(), scanResult.getScanRecord().getBytes());
                    }

                    @Override
                    public void onBatchScanResults(List<ScanResult> list) {
                        for (int i = 0; BleDeviceScanner.this.scannerCallback != null && BleDeviceScanner.this.scannerHandler != null && i < list.size(); i++) {
                            ScanResult scanResult = list.get(i);
                            BleDeviceScanner.this.handleDeviceForBle(scanResult.getDevice(), scanResult.getRssi(), scanResult.getScanRecord().getBytes());
                        }
                    }
                };
            }
            ArrayList arrayList = null;
            if (filterUUIDS != null) {
                arrayList = new ArrayList();
                for (UUID uuid : filterUUIDS) {
                    arrayList.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(uuid)).build());
                }
            }
            bluetoothLeScanner.startScan(arrayList,
                    new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build(), this.scanCallbackForHigherThan20);
        }
        return bluetoothLeScanner != null;
    }

    protected void handleDeviceForBle(BluetoothDevice bluetoothDevice, int i, byte[] bArr) {
        String address = bluetoothDevice.getAddress();
        String name = bluetoothDevice.getName();
        if (TextUtils.isEmpty(address) || TextUtils.isEmpty(name) || !shouldNotify(address)) {
            return;
        }
        notifyScannedDevice(new ScannedDeviceResult(bluetoothDevice, i, bArr));
    }

    @Override
    public void release() {
        stopScan();
        this.scanCallbackForHigherThan20 = null;
        this.scanCallbackForLowerThan21 = null;
        super.release();
    }
}
